﻿using System;
using System.Collections.Generic;
using System.Windows.Data;

namespace Trade.Journal
{
    /// <summary>
    /// 一次交易
    /// </summary>
    public class DealViewModel : BindableBase
    {
        public DealViewModel()
        {
            PhotoSource.Filter += PhotoSource_Filter;
        }

        private void PhotoSource_Filter(object sender, FilterEventArgs e)
        {
            if (e.Item is PhotoInfo p && p.RecordId == RecordId && p.DealId == DealId)
                e.Accepted = true;
            else
                e.Accepted = false;
        }

        public bool UpdateToDatabase { get; set; }

        public virtual int Sequence { get; set; }

        public string RecordId { get; set; }

        public string DealId { get; set; } = Guid.NewGuid().ToString("N");

        /// <summary>
        /// 类型
        /// 入场 加仓 减仓 离场 
        /// </summary>
        public virtual TradeType TradeType { get; }


        private DateTime? _Time;//= DateTime.Now;
        /// <summary>
        /// 交易时间
        /// </summary>  
        public DateTime? Time
        {
            get { return _Time; }
            set { SetProperty(ref _Time, value); if (UpdateToDatabase) this.Update(value); }
        }


        private double? _Price;
        /// <summary>
        /// 价格
        /// </summary>  
        public double? Price
        {
            get { return _Price; }
            set { SetProperty(ref _Price, value); if (UpdateToDatabase) this.Update(value); }
        }


        /// <summary>
        /// 手数
        /// </summary>
        private double? _Lots;

        public double? Lots
        {
            get { return _Lots; }
            set { SetProperty(ref _Lots, value); }
        }


        /// <summary>
        /// 理由
        /// </summary>  
        //public ObservableCollection<string> Reasons { get; } = new ObservableCollection<string>();
        public List<string> Reasons { get; } = new List<string>();

        ///// <summary>
        ///// 疑问
        ///// </summary>
        //public List<string> Questions { get; } = new List<string>();

        ///// <summary>
        ///// 心得
        ///// </summary>
        //public List<string> Lessons { get; } = new List<string>();

        ///// <summary>
        ///// 标签
        ///// </summary>
        //public List<string> Marks { get; } = new List<string>();

        public CollectionViewSource PhotoSource { get; } = new CollectionViewSource();

        public BarCycle TradeCycle { get; set; }

    }

    public class OpenDealViewModel : DealViewModel
    {
        public override TradeType TradeType => TradeType.Open;

        public override int Sequence { get => 1; set => throw new NotSupportedException(); }


        public static TradeDirection[] OpenDirections { get; } = new TradeDirection[] { TradeDirection.Long, TradeDirection.Short };

        private TradeDirection _OpenDirection;

        public TradeDirection OpenDirection
        {
            get { return _OpenDirection; }
            set { SetProperty(ref _OpenDirection, value); }
        }



    }


    public class UnderweightDealViewModel : DealViewModel
    {
        public override TradeType TradeType => TradeType.Underweight;

        public static CloseType[] CloseTypes { get; } = new CloseType[] { CloseType.KBar, CloseType.Measure, CloseType.MA };

        public CloseType CloseType { get; set; }

        public double Highest { get; set; }

        public double Lowest { get; set; }
    }

    public class CloseDealViewModel : UnderweightDealViewModel
    {
        public static ResultType[] ResultTypes { get; } = new ResultType[] { ResultType.Success, ResultType.BreakEven, ResultType.Fail };

        public CloseDealViewModel()
        {
            CloseType = CloseType.MA;
        }

        public override TradeType TradeType => TradeType.Close;

        public ResultType ResultType { get; set; }

    }
}
